#!/bin/bash
set -e

if ! grep "# added by clearwater-*/hooks/install" /etc/hosts ; then
  private_ip=$(unit-get private-address)
  echo "$private_ip homer-$(cut -d/ -f2 <<< $JUJU_UNIT_NAME).$(config-get zone) # added by clearwater-*/hooks/install" >>/etc/hosts
fi

# Install chef solo
$CHARM_DIR/lib/chef_solo_install

# Get the ip address and machine number of this unit. The index
# takes the form '<charm name>/<index>
this_ip=$(unit-get private-address)
this_index=$(echo $JUJU_UNIT_NAME | cut -d'/' -f2)

$CHARM_DIR/lib/node_json_clustered_script --ip "$this_ip" --index "$this_index"
$CHARM_DIR/lib/node_json_script

# Update the config file
$CHARM_DIR/lib/config_script

apt-get update

# After we install dnsmasq, DNS might drop for a few seconds.  Install it ahead of time
# and wait for DNS to recover.
apt-get -q -y --force-yes install dnsmasq
iterations=0
while [ $(dig +short security.ubuntu.com | wc -l) == 0 ] && [ $iterations -lt 60 ] ; do
  echo "Waiting for DNS to security.ubuntu.com to recover ($iterations/60)..."
  sleep 1
  iterations=$((iterations + 1))
done

# Install the node
chef-solo -c /home/ubuntu/chef-solo/solo.rb -j /home/ubuntu/chef-solo/node.json

# Expose the correct ports
open-port 22/tcp
open-port 123/udp
open-port 161/udp
